package org.devio.`as`.proj.common.view

import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry
import io.flutter.plugin.common.PluginRegistry

/*
* 1.如何获取PluginRegistry.Registrar
* 2.非继承FlutterActivity时如何注册native view
* */
object HiImageViewPlugin {

    var HI_IMAGE_VIEW = "HiImageView" //原生控件对应的 view type id

    ///兼容1.12以前旧版本
    fun registerWith(registry: PluginRegistry) {
        val key: String = HiImageViewFactory::class.java.canonicalName!!
        if (registry.hasPlugin(key)) {
            return
        }
        val registrar = registry.registrarFor(key)
        registrar.platformViewRegistry()
            .registerViewFactory(HI_IMAGE_VIEW, HiImageViewFactory(registrar.messenger()))
    }

    /// 新版本注册方式
    fun registerWith(flutterEngine: FlutterEngine?) {
        val key: String = HiImageViewPlugin::class.java.canonicalName!!
        val shimPluginRegistry = ShimPluginRegistry(flutterEngine!!)
        if (shimPluginRegistry.hasPlugin(key)) {
            return
        }

        val registrar = shimPluginRegistry.registrarFor(key)
        registrar.platformViewRegistry()
            .registerViewFactory(HI_IMAGE_VIEW, HiImageViewFactory(registrar.messenger()))
    }
}